package com.devilvan.jsoup;

import org.apache.http.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.util.Map;

public class HttpClientUtil {
    private static final String CONTENT_TYPE_TEXT_JSON = "text/json";

    public static String httpGetByJson(String url, String json, Map<String,String> header) throws Exception {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Accept",header.get("Accept"));
        httpPost.setHeader("Accept-Encoding",header.get("Accept-Encoding"));
        httpPost.setHeader("Referer",header.get("Referer"));
        httpPost.setHeader("Host",header.get("Host"));
        httpPost.setHeader("Origin",header.get("Origin"));
        httpPost.setHeader("Connection",header.get("Connection"));
        httpPost.setHeader("Content-Type",header.get("Content-Type"));
        httpPost.setHeader("User-Agent",header.get("User-Agent"));
        httpPost.setHeader("Cookie",header.get("Cookie"));
        StringEntity se = new StringEntity(json);
        se.setContentType(CONTENT_TYPE_TEXT_JSON);
        httpPost.setEntity(se);
        CloseableHttpResponse response2 = client.execute(httpPost);
        HttpEntity entity2 = response2.getEntity();
        String s2 = EntityUtils.toString(entity2, "UTF-8");
        return s2;
    }
}
